package cn.news.xinhuanet.lottery;

import cn.news.xinhuanet.lottery.pool.PrizeIndex;
import cn.news.xinhuanet.lottery.pool.PrizePool;
import org.apache.log4j.PropertyConfigurator;

import java.net.URL;

/**
 * Created by xinhuanet on 16-2-14.
 */
public class PoolOperaUtil {


    static {
        URL url = PoolOperaUtil.class.getClassLoader().getResource("cn/news/xinhuanet/config/log4j.properties");
        System.out.println(url.getPath());
        PropertyConfigurator.configure(url);
    }

    public static void initPrizePool() {
        for (int i = 0; i < 100; i++) {
            PrizePool.setPrize(i, 0);//初始化奖品池100个“谢谢参与”奖
        }
        PrizePool.setPrize(PrizePool.getPrizeSize(), 1);//初始化奖品池100个“谢谢参与”奖
        PrizePool.setPrize(PrizePool.getPrizeSize(), 1);//初始化奖品池100个“谢谢参与”奖
    }


    public static void main(String[] args) {
        PrizeIndex.setRandomIndex(false);//false:顺序抽奖，true：随即抽奖
        PoolOperaUtil.initPrizePool();//初始化奖品池

        PrizeThread prizeThread = new PrizeThread();
        for (int i = 0; i < 100; i++) {
            Thread thread = new Thread(prizeThread);
            thread.start();
        }
    }
}

class PrizeThread implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            PrizePool.getPrize();
            if (i % 3 == 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
